﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1.Daywtwo
{
    public class Sqlhyper
    {
        private static string consql = "server=.;uid=sa;pwd=123456;database=Student_db";

        private SqlConnection con = null;

        public Sqlhyper() {
           con= new SqlConnection(consql);
        }
        public DataTable Gettable(string sql,SqlParameter[] p) {
            try
            {

                SqlCommand command = new SqlCommand(sql, con);

                if (p != null)
                {
                    command.Parameters.AddRange(p);
                }
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataSet set = new DataSet();
                adapter.Fill(set);
                return set.Tables[0];
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally {
                if (con!=null)
                {
                    con.Close();
                }
            }       
        }

        public bool NotFind(string sql,SqlParameter[] p) {
            try
            {
                SqlCommand command = new SqlCommand(sql, con);
                if (p != null)
                {
                    command.Parameters.AddRange(p);
                }
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                return command.ExecuteNonQuery() > 0;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally {
                if (con!=null)
                {
                    con.Close();
                }
            }
        }

    }
}